package com.zzl.boot.bigevent.controller;

import com.zzl.boot.bigevent.pojo.*;
import com.zzl.boot.bigevent.service.articleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/article")
@CrossOrigin//允许跨区请求
@Slf4j
public class articleController {
    @Autowired
    private articleService service;
    //添加文章
    @PostMapping()
    public result add(@RequestBody @Validated(article.add.class) article article) {
        service.addArticle(article);
        return result.success();
    }
    //分页查询文章
    @GetMapping()
    public result<PageBean<article>> getPage(Integer pageNum, Integer pageSize,
                                             @RequestParam(required = false) Integer id,
                                             @RequestParam(required = false) String state) {
        PageBean<article> pb= service.getClassPages(pageNum,pageSize,id,state);
        return result.success(pb);
    }
    //获取文章详情
    @GetMapping("/info")
    public result<article> getById( Integer id) {
        article article= service.getById(id);
        //System.out.println(article);
        return result.success(article);
    }
    //更新文章详情
    @PutMapping("/info")
    public result update(@RequestBody @Validated(article.update.class) article article) {
        service.update(article);
        return result.success();
    }
    //删除文章
    @DeleteMapping()
    public result delete(Integer id) {
        service.del(id);
        return result.success();
    }
    //根据分类删除
    @DeleteMapping("/byCate")
    public result deleteByCate(Integer categoryId) {
        service.delByCate(categoryId);
        return result.success();
    }
    //根据分类获取文章id
    @GetMapping("/getArcId")
    public result<Integer> getByArcId() {
        Integer id=service.getArcId();
        return result.success(id);
    }
    //根据用户id获取文章列表
    @GetMapping("/listByUser")
    public result<List<article>> listByUser() {
        List<article> list=service.listByUser();
        return result.success(list);
    }
    //分页获取所有文章
    @GetMapping("/list")
    public result<PageBean<article>> list(Integer pageNum, Integer pageSize) {
        PageBean<article> pageBean=service.getList(pageNum,pageSize);
        return result.success(pageBean);
    }
    //获取某个分类有几篇文章在引用
    @GetMapping("/categoryCount")
    public result<Integer> categoryCount(Integer id) {
        Integer count= service.getCategoryCount(id);
        return result.success(count);
    }
    //根据文章分类获取文章列表
    @GetMapping("/listByCategory")
    public result<List<article>> listByCategory(Integer categoryId) {
        List<article> list =service.listByCategory(categoryId);
        return result.success(list);
    }
    //增加文章访问量
    @GetMapping("/addView")
    public result addView(Integer id) {
        service.addView(id);
        return result.success();
    }
    //模糊查询文章
    @GetMapping("/search")
    public result<List<article>> search(String keyword) {
        List<article> list=service.search(keyword);
        return result.success(list);
    }
    //获取用户信息
    @GetMapping("/user")
    public result<String> user(Integer id) {
        String user= service.findUser(id);
        return result.success(user);
    }
}
